package com.forum.framework.common.exception.util;

import com.forum.framework.common.exception.ErrorCode;
import com.forum.framework.common.exception.ServiceException;
import com.forum.framework.common.exception.enums.GlobalErrorCodeConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ServiceException 的工具类
 *
 * @author zihan.ouyang
 */
public class ServiceExceptionUtils {
    private static final Logger logger = LoggerFactory.getLogger(ServiceExceptionUtils.class);

    // ========== 和 ServiceException 的集成 ==========
    public static ServiceException exception(ErrorCode errorCode) {
        return exception0(errorCode.getCode(), errorCode.getMsg());
    }

    public static ServiceException exception(ErrorCode errorCode, Object... params) {
        return exception0(errorCode.getCode(), errorCode.getMsg(), params);
    }

    public static ServiceException exception0(Integer code, String messagePattern, Object... params) {
        String message = doFormat(code, messagePattern, params);
        return new ServiceException(code, message);
    }

    public static ServiceException invalidParamException(String messagePattern, Object... params) {
        return exception0(GlobalErrorCodeConstants.BAD_REQUEST.getCode(), messagePattern, params);
    }

    // ========== 格式化方法 ==========

    /**
     * 将错误编号对应的消息使用 params 进行格式化。
     *
     * @param code           错误编号
     * @param messagePattern 消息模版
     * @param params         参数
     * @return 格式化后的提示
     */
    private static String doFormat(int code, String messagePattern, Object... params) {
        StringBuilder sb = new StringBuilder(messagePattern.length() + 50);
        int i = 0;
        int j;
        for (int l = 0; l < params.length; l++) {
            j = messagePattern.indexOf("{}", i);
            if (j == -1) {
                logger.error("[doFormat][参数过多：错误码({})|错误内容({})|参数({})", code, messagePattern, params);
                if (i == 0) {
                    return messagePattern;
                } else {
                    sb.append(messagePattern.substring(i));
                    return sb.toString();
                }
            } else {
                sb.append(messagePattern, i, j);
                sb.append(params[l]);
                i = j + 2;
            }
        }
        if (messagePattern.indexOf("{}", i) != -1) {
            logger.error("[doFormat][参数过少：错误码({})|错误内容({})|参数({})", code, messagePattern, params);
        }
        sb.append(messagePattern.substring(i));
        return sb.toString();
    }
}
